package com.wngbms.websocket.zookeeper;

import com.wngbms.websocket.entity.UserMetaInfo;
import com.wngbms.websocket.notice.NoticeRemoteUser;
import com.wngbms.websocket.user.RedisUserMetaInfoHandler;
import com.wngbms.websocket.user.UserHook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserNodeNotifier implements ZkConst {

    @Autowired
    ZookeeperSupport zookeeperSupport;
    @Autowired
    UserHook userHook;
    @Autowired
    RedisUserMetaInfoHandler redisUserMetaInfoHandler;
    @Autowired
    NoticeRemoteUser feignNoticeRemoteUser;

    public void notifyUser(String userId, String eventId) throws Exception {
        UserMetaInfo userMetaInfo = redisUserMetaInfoHandler.getUserMetaInfo(userId);
        if (userMetaInfo == null) {
            log.warn("userMetaInfo not exist {}", userId);
            return;
        }

        if (SERVICE_UUID.equals(userMetaInfo.getServiceId())) {
            userHook.doEvent(eventId, userId);
        } else {
            zookeeperSupport.createEphemeral(getUserServiceRootPath(userId, eventId, userMetaInfo.getServiceId()),
                    getPushEventContent(eventId, userId));
        }
    }

    public void forceCloseRemoteUser(UserMetaInfo userMetaInfo) {
        feignNoticeRemoteUser.noticeRemoteUser(userMetaInfo);
    }

}
